﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Common.DataTypes;
using System.Xml;
using System.IO;
using Common;
using System.Xml.Serialization;

namespace LINQAmazonProvider
{
    public static class ResultXMLParser
    {
        public static AmazonBookCollection ParseXmlQueryResult(string xmlResult)
        {
            AmazonBookCollection ABookCollection = null;

            try
            {
                XmlReader xmlReader = XmlReader.Create(new StringReader(xmlResult));

                XmlSerializer xSerializer = new XmlSerializer(typeof(AmazonBookCollection));

                ABookCollection = (AmazonBookCollection)xSerializer.Deserialize(xmlReader);
            }
            catch (Exception e)
            {
            }

            return (ABookCollection);
        }

        public static int GetTotalPagesQueryResult(string xmlResult) {
            AmazonBookCollection ABookCollection = ResultXMLParser.ParseXmlQueryResult(xmlResult);

            return (ABookCollection.ResultData.TotalPages);
        }

        public static object GetQueryResult(List<string> serializedObj, Dictionary<string, string> parameters)
        {
            Object result = null;
            AmazonBookCollection ABookCollection = null;
            List<Book> listResult = new List<Book>();
            string method = parameters["Method"];

            try
            {
                foreach (string xmlResult in serializedObj)
                {
                    ABookCollection = ResultXMLParser.ParseXmlQueryResult(xmlResult);

                    listResult.AddRange(ABookCollection.GetBooks());
                }

                switch (method) {
                    case "Count" :
                        result = ABookCollection.GetTotalResults();
                        break;
                    case "First" :
                        if (listResult.Count > 0)
                        {
                            result = listResult[0];
                        }
                        else {
                            result = null;
                        }
                        
                        break;
                    case "Last":
                        if (listResult.Count > 0)
                        {
                            if (parameters.ContainsKey("Take"))
                            {
                                result = listResult[int.Parse(parameters["Take"]) - 1];
                            }
                            else {
                                result = listResult[listResult.Count - 1];
                            }
                        }
                        else
                        {
                            result = null;
                        }

                        break;
                    default :
                        if (parameters.ContainsKey("Take"))
                        {
                            result = listResult.GetRange(0, int.Parse(parameters["Take"]));
                        }
                        else
                        {
                            result = listResult;
                        }
                        break;
                }
            }
            catch (Exception e)
            {
            }
            return (result);
        }
    }
}
